package cn.tedu.medicalappointmentsystemk.response;

import cn.tedu.medicalappointmentsystemk.handler.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class JsonResult implements Serializable {
    @ApiModelProperty("业务状态码")
    private Integer code;

    @ApiModelProperty("提示文本")
    private String message;

    @ApiModelProperty("数据")
    private Object data;

    /**
     * 生成表示"失败"的响应对象
     *
     * @param e 业务异常
     * @return 表示"失败"的响应对象
     */
    public static JsonResult fail(ServiceException e) {
        return fail(e.getServiceCode(), e.getMessage());
    }

    /**
     * 生成表示"失败"的响应对象
     *
     * @param serviceCode 业务状态码
     * @param message     提示文本
     * @return 表示"失败"的响应对象
     */
    public static JsonResult fail(ServiceCode serviceCode, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.code = serviceCode.getValue();
        jsonResult.message = message;
        return jsonResult;
    }

    public static JsonResult ok(Object data){
        JsonResult jsonResult = new JsonResult();
        jsonResult.code=ServiceCode.OK.getValue();
        jsonResult.data = data;
        return jsonResult;
    }

    public static JsonResult ok(){
        return ok(null);
    }
}
